﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace SharpShare.Storage.Zip {
    internal class ZipEntryStreamStorageDataProvider : StreamStorageDataProvider, IStaticDataSource {
        public ZipEntryStreamStorageDataProvider(Stream baseStream, ZipArchiveStorageItem file)
            : base(baseStream) {
                this.File = file;
        }

        public ZipArchiveStorageItem File { get; private set; }

        public override void Close() {
            try {
                this.Stream.Seek(0, SeekOrigin.Begin);

                ZipArchiveStorageItem zipItem = (ZipArchiveStorageItem)this.File;
                zipItem.BeginUpdatingArchive(() => {
                    zipItem.ZipFile.Add(this, zipItem.Path);
                });
            } finally {
                base.Close();
            }
        }

        #region IStaticDataSource Members

        Stream IStaticDataSource.GetSource() {
            return this.Stream;
        }

        #endregion
    }
}
